package com.yuanqiao.insight.modules.notice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yuanqiao.insight.modules.notice.entity.SysNoticeConfig;
import com.yuanqiao.insight.modules.notice.entity.SysNoticeExtend;
import com.yuanqiao.insight.modules.notice.mapper.SysNoticeConfigMapper;
import com.yuanqiao.insight.modules.notice.service.ISysNoticeConfigService;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 通知
 * @Author: jeecg-boot
 * @Date:   2021-05-27
 * @Version: V1.0
 */
@Service
public class SysNoticeConfigServiceImpl extends MPJBaseServiceImpl<SysNoticeConfigMapper, SysNoticeConfig> implements ISysNoticeConfigService {

    @Override
    public JSONObject getNoticeConfig(String configId) {
        MPJLambdaWrapper<SysNoticeConfig> wrapper = new MPJLambdaWrapper<SysNoticeConfig>()
                .selectAll(SysNoticeConfig.class)
                .selectCollection(SysNoticeExtend.class, SysNoticeConfig::getExtend)
                .leftJoin(SysNoticeExtend.class, SysNoticeExtend::getNoticeConfigId, SysNoticeConfig::getId)
                .eq(SysNoticeConfig::getId, configId);
        SysNoticeConfig noticeConfig = this.selectJoinOne(SysNoticeConfig.class, wrapper);
        return getJsonObject(noticeConfig);
    }

    @NotNull
    private static JSONObject getJsonObject(SysNoticeConfig noticeConfig) {
        final List<SysNoticeExtend> noticeExtends = noticeConfig.getExtend();
        JSONObject sendMsg = new JSONObject();
        for (SysNoticeExtend noticeExtend : noticeExtends) {
            if (StringUtils.isEmpty(noticeExtend.getFieldValue())) {
                sendMsg.put(noticeExtend.getFieldCode(), "");
            } else {
                sendMsg.put(noticeExtend.getFieldCode(), noticeExtend.getFieldValue());
            }
        }
        JSONObject result = new JSONObject();
        result.put("support", noticeConfig.getConfigType());
        result.put("sendMsg", sendMsg);
        return result;
    }
}
